<?php

namespace app\admin\controller;
use app\admin\model\OrderModel;
use think\Db;
use org\Qiniu;

class Order extends Base
{
    /**
     * [index 用户列表]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['telephone|realname'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['create_time'] = ['between time',[$start,$end]];
            }
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od=$field." ".$order;
            }else{
                $od="create_time desc";
            }
            $config = new OrderModel();
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;
            $lists = $config->getAllConfig($map, $Nowpage, $limits,$od);
            $count = $config->getAllCount($map);//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("order/index");
    }

    /**
     * [add_config 添加配置]
     */
    public function add_config()
    {
        if(request()->isPost()){
            extract(input());
            $param = input('post.');
            $param['create_time'] = date('Y-m-d H:i:s',time());
            if(!isset($status)){
                $param['status'] = 1;
            }
            // dump($param);die;
            $config = new OrderModel();
            $flag = $config->insertConfig($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch("seller/add_config");
    }

    //tableSelect测试数据
    public function getUserData(){
        if(request()->isGet ()){
            extract(input());
            $map = [];
            if(isset($keyword)&&$keyword!=""){
                $map['name'] = ['like',"%" . $keyword . "%"];
            }
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $count = Db::name('test')->where($map)->count();//计算总页面
            $lists = Db::name('test')
                ->where($map)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(request()->isPost ()){
            $data = Db::name('test')
                ->where('id','in',input('id'))
                ->select();
            return json(['code'=>200,'data'=>$data]);
        }
    }


    /**
     * [add_article 添加文章]
     * @return [type] [description]
     * @author
     */
    public function add_article()
    {
        if(request()->isPost()){
            extract(input());
            $param = input('post.');
            if(!isset($status)){
                $param['status'] = 2;
            }
            $param['photo'] = trim($param['photo'],',');
            $article = new ArticleModel();
            $flag = $article->insertArticle($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $cate = new ArticleCateModel();
        $area = new \app\common\place\Area;
        return $this->fetch('article/add_article',['province'=>$area->province(),'cate'=>$cate->getCate()]);
    }
    /**
     * [user_state 配置状态]
     * @author
     */
    public function status_config()
    {
        extract(input());
        $config = new OrderModel();
        $flag = $config->configState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [edit_config 编辑配置]
     */
    public function edit_config()
    {
        $config = new OrderModel();
        if(request()->isPost()){
            $param = input('post.');
            $flag = $config->editConfig($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $info = $config->getOneConfig($id);
        $this->assign('info', $info);
        return $this->fetch();
    }

/**
     * [del_config 删除配置]
     * @author
     */
    public function del_config()
    {
        $id = input('param.id');
        $config = new OrderModel();
        $flag = $config->delConfig($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }


    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelAdmin(){
        extract(input());
        $map = [];
        if($key!="")
        {
            $map['order_no'] = ['like',"%" . $key . "%"];
        }
        if($start!=""&&$end=="")
        {
            $map['create_time'] = ['>= time',$start];
        }
        if($end!=""&&$start=="")
        {
            $map['create_time'] = ['<= time',$end];
        }
        if($start!=""&&$end!="")
        {
            $map['create_time'] = ['between time',[$start,$end]];
        }

        $data = Db::name('order')->where($map)->select();
        foreach ($data as $k => $value) {
            $data[$k]['sellor_name'] = Db::name('seller')->where('id',$value['sellor_id'])->value('sellor_name');
            if($value['type_id'] == 1){
                $data[$k]['type_name'] = '寺库分期';
            }else if($value['type_id'] == 2){
                $data[$k]['type_name'] = '乐百分分期';
            }
            $data[$k]['contract'] = $data[$k]['contract'].'期';
        } 
        $cellname = [
            ['id','ID',15,'LEFT'],
            ['order_no','订单号',25,'LEFT'],
            ['sellor_name','来源商家',25,'LEFT'],
            ['type_name','支付方式',25,'LEFT'],
            ['contract','签约期数',10,'LEFT'],
            ['total_price','商品总金额',15,'LEFT'],
            ['invoice_bankname','开户银行',25,'LEFT'],
            ['invoice_bankcardid','开户银行账号',25,'LEFT'],
            ['create_time','创建时间',35,'LEFT'],
            ['update_time','修改时间',15,'LEFT']
        ];
        $res = exportExcel('订单信息','admin',$cellname,$data);
        return json($res);
    }

}